<script>
    function resolveDate(data) {
        var arr = [];
        for (var i in data) {
            arr.push(i + '=' + data[i])
        }
        return arr.join('&')
    }
    function ajax(obj) {
        const xhr = new XMLHttpRequest();
        const dt = resolveDate(obj.data);
        //判断请求
        if (obj.type.toUpperCase() === 'GET') {
            xhr.open(obj.type, obj.url + '?' + dt)
            xhr.send();
        } else if (obj.type.toUpperCase() === 'POST') {
            xhr.open(obj.type, obj.url);
            xhr.setRequestHeader('Content-Type', 'application/x-www/form-urlencoded');
            xhr.send(dt);
        }
        // 监听xhr对象请求状态
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                var result = JSON.parse(xhr.responseText);
                obj.success(result);
            }
        }
    }
    ajax({
        type: "get",
        url: "http://www.liulongbin.top:3006/api/getbooks",
        success: function (res) {
            console.log(res);
        }
    })
    ajax({
        type: "post",
        url: "http://www.liulongbin.top:3006/api/addbook",
        data: {
            bookname: "cnm",
            author: "ccc",
            pulisher: "ssss"
        },
        success: function (res) {
            console.log(res);
        }
    })
</script>